package com.fhnew.app.widget;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.view.View;
import android.widget.GridLayout;

import java.util.ArrayList;
import java.util.List;

public class GameListView extends GridLayout {

    private List<View> games;

    private List<ObjectAnimator> objectAnimatorTranslateYs;

    private int rootWidth;

    public GameListView (Context context) {
        super(context);
        init();
    }

    public GameListView (Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public GameListView (Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        objectAnimatorTranslateYs = new ArrayList<>();
    }





    @Override
    protected void onMeasure(int widthSpec, int heightSpec) {
        int rowCount =  getRowCount();
        if(rowCount == 0 ||games == null){
            setMeasuredDimension(widthSpec,heightSpec);
            return;
        }

        int widthModel = MeasureSpec.getMode(widthSpec);
        int heightModel = MeasureSpec.getMode(heightSpec);

        rootWidth = MeasureSpec.getSize(widthSpec);
        int rootHeight = MeasureSpec.getSize(heightSpec);


        int widthCount = 0;

        for (int i = 0; i < getChildCount(); i++) {
            View view = getChildAt(i);
            LayoutParams layoutParams = (LayoutParams) view.getLayoutParams();

            int anailableHight = (rootHeight-layoutParams.bottomMargin*rowCount-layoutParams.topMargin*rowCount);

            int childWidth = layoutParams.width;
            int childHeight = anailableHight/rowCount;
            if(childWidth <= 0){
                if(rowCount > 1){
                    childWidth = anailableHight/rowCount;
                }else{
                    //420/630 是单张图片的比例
                    childWidth = (int)((float)anailableHight*(420.0/630.0));
                }
            }

            int childWidthSpec = MeasureSpec.makeMeasureSpec(childWidth, MeasureSpec.EXACTLY);
            int childHeightSpec = MeasureSpec.makeMeasureSpec(childHeight, MeasureSpec.EXACTLY);

            view.measure(childWidthSpec,childHeightSpec);

            if(i%rowCount==0){ //0%2    1%2         2%2
                widthCount += view.getMeasuredWidth()+layoutParams.rightMargin+layoutParams.leftMargin;
            }
        }

        widthSpec = MeasureSpec.makeMeasureSpec( widthCount>rootWidth?widthCount:rootWidth ,widthModel);
        heightSpec = MeasureSpec.makeMeasureSpec(rootHeight,heightModel);

        setMeasuredDimension(widthSpec,heightSpec);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    public void setGamesView(List<View> games){
        this.games = games;
        for (int i = 0; i < games.size(); i++) {
            addView(games.get(i));
        }

        if(this.games == null || this.games.size() != games.size()){
            requestLayout();
        }

    }

    private int  index =0;
    public void startAnimator(){
        int  count =getChildCount();
        if(count == 0)return;
        index = 0;
        for (int i = 0; i < count; i++) {
            getChildAt(i).setVisibility(View.GONE);
        }
        beginAnimation();
    }

    private void beginAnimation(){
        View view = getChildAt(index++);
        view.setVisibility(View.VISIBLE);
        ObjectAnimator translateX = ObjectAnimator.ofFloat(view, "translationX", rootWidth, 0);
        translateX.setDuration(300);
        translateX.start();
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                if(index< getChildCount() && index <12){
                    beginAnimation();
                }else if(index >= 12){
                    for (int i = 11; i < getChildCount(); i++) {
                        View view = getChildAt(i);
                        view.setVisibility(View.VISIBLE);
                    }
                }
            }
        },60);
    }


}
